import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_scan_tool/bloc/device/device_bloc.dart';
import 'package:flutter_scan_tool/bloc/tool/tool_bloc.dart';
import 'package:flutter_scan_tool/bloc/tool/tool_event.dart';
import 'package:flutter_scan_tool/models/app_theme.dart';
import 'package:select_form_field/select_form_field.dart';

class SearchPage extends StatefulWidget {
  const SearchPage({Key? key}) : super(key: key);

  @override
  State<SearchPage> createState() => _SearchPageState();
}

class _SearchPageState extends State<SearchPage> {
  final _toolName = TextEditingController();
  final _storageName = TextEditingController();
  List<Map<String, dynamic>> list = [];
  FocusNode focusNode = FocusNode();

  @override
  void initState() {
    super.initState();
    DeviceBloc bloc = context.read<DeviceBloc>();
    final itemList = bloc.storageInfo?.toolKitList;
    if (itemList != null && itemList.isNotEmpty) {
      for (var item in itemList) {
        list.add({'label': item.toolKitName, 'value': item.id});
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    final size = MediaQuery.of(context).size;
    return body(size);
  }

  Widget body(Size size) {
    return Container(
      margin: const EdgeInsets.only(top: 10,left: 10,right: 10),
      child: SingleChildScrollView(
        child: Container(
          width: size.width * 0.85,
          height: size.height * 0.33,
          decoration: BoxDecoration(
            shape: BoxShape.rectangle,
            borderRadius: const BorderRadius.all(Radius.circular(20)),
            color: AppTheme.appTheme.cardBackgroundColor(),
          ),
          padding: const EdgeInsets.only(top: 10, left: 10, right: 10,bottom: 10),
          child: Scaffold(
            body: Column(
              children: [
                Container(
                  margin: const EdgeInsets.all(10),
                  padding: const EdgeInsets.only(left: 10, right: 10),
                  decoration: const BoxDecoration(
                      color: Color(0xfff1e6ff),
                      borderRadius: BorderRadius.all(Radius.circular(15))),
                  child: TextField(
                    // focusNode: focusNode,
                    autofocus: false,
                    decoration: const InputDecoration(
                        icon: Icon(
                          Icons.person,
                          color: Color(0xff6f35a5),
                        ),
                        hintText: '请输入工具名称',
                        border: InputBorder.none),
                    controller: _toolName,
                  ),
                ),
                Container(
                  margin: const EdgeInsets.all(10),
                  padding: const EdgeInsets.only(left: 10, right: 10),
                  decoration: const BoxDecoration(
                      color: Color(0xfff1e6ff),
                      borderRadius: BorderRadius.all(Radius.circular(15))),
                  child: SelectFormField(
                    type: SelectFormFieldType.dropdown,
                    decoration: const InputDecoration(
                        icon: Icon(
                          Icons.select_all_sharp,
                          color: Color(0xff6f35a5),
                        ),
                        border: InputBorder.none,
                        hintText: '请选择工具柜'),
                    controller: _storageName,
                    items: list,
                  ),
                ),
                Container(
                  width: double.infinity,
                  margin: const EdgeInsets.all(10),
                  child: ElevatedButton(
                      onPressed: () {
                        Map<String, dynamic> map = {};
                        if (_toolName.text.isNotEmpty) {
                          map.putIfAbsent('name', () => _toolName.text);
                        }
                        if (_storageName.text.isNotEmpty) {
                          map.putIfAbsent(
                              'toolKitList', () => [_storageName.text]);
                        }
                        context.read<ToolBloc>().add(ToolLoadEvent(map));
                        Navigator.pop(context);
                      },
                      child: Row(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: const <Widget>[
                            Icon(Icons.search),
                            SizedBox(
                              width: 10,
                            ),
                            Text('搜索')
                          ])),
                )
              ],
            ),
          ),
        ),
      ),
    );
  }
}
